package hot_100;

/**
 * @author: Darven
 * @createTime: 2025-10-03  22:24
 * @description: TODO
 */
public class P105 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        // 先序遍历首位是root，然后去查中序遍历的左右节点
        TreeNode root=null;
        for (int i=0,j=0;i<preorder.length;){
            int rootValue = preorder[i];
            // 找到左右节点
            while (inorder[j]!=rootValue){
                j++;
            }
            // 左节点值
            TreeNode left=null;
            if(preorder[i+1]==inorder[j-1]){
                left=new TreeNode(preorder[i+1]);
                i++;
            }
            // 右节点值
            TreeNode right=null;
            if(preorder[i+1]==inorder[j+1]){
                right=new TreeNode(inorder[i+1]);
                j++;
            }
            root=new TreeNode(rootValue, left, right);
        }
        return null;
    }
}
